package com.hzhr.iot.core.util;

import java.util.List;
import java.util.Map;

public class HzhrHttpUtil {
    public static String queryMap2url(Map<String, Object> queryMap) {
        String result = "";
        if (queryMap == null || queryMap.size() == 0) return result;

        for (Map.Entry<String, Object> entry :
        queryMap.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();

            if (value != null) {
                if (value instanceof List) {
                    String valueStr = "";
                    List valueList = (List) value;
                    int i = 0;
                    for (Object item:
                            valueList) {
                        if (i == valueList.size() - 1) {
                            valueStr += item;
                        } else {
                            valueStr += item + ",";
                        }

                        i++;
                    }

                    result += key + "=" + valueStr + "&";
                } else {
                    result += key + "=" + value.toString() + "&";
                }
            }
        }

        if (result.length() > 0) {
            result = result.substring(0, result.length() - 1);
        }
        return result;
    }
}
